from fastapi import APIRouter, HTTPException
from .models import OutSpeed, OutChange, InSpeed, InChange
from .sub_utils import CurtainControl
from ..async_tcp_server import server, serverInQueue, serverOutQueue
import asyncio
import time

router = APIRouter(
    prefix="/CurtainControl",
    tags=["窗帘控制"]
)

dbCurtain = CurtainControl()


@router.post("/Change", response_model=OutChange)
async def curtainChange(change: InChange):
    # await asyncio.sleep(1)
    if type(change.status) is not bool:
        raise HTTPException(status_code=412, detail='Api data handle fault!')
    await dbCurtain.setOpenStatus(change.status)
    return {'status': dbCurtain.getOpenStatus(), 'time': time.time() * 1000}


@router.post("/Speed", response_model=OutSpeed)
async def curtainSpeed(speed: InSpeed):
    if speed.mode == 1:
        await dbCurtain.setExtraSpeed('慢')
    elif speed.mode == 2:
        await dbCurtain.setExtraSpeed('中')
    elif speed.mode == 3:
        await dbCurtain.setExtraSpeed('快')
    else:
        raise HTTPException(status_code=412, detail='Api data handle fault!')
    return {'index': dbCurtain.getExtraSpeed(), 'time': time.time() * 1000}
